﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ShapeDrawer
{
    public enum State
    {
        Normal,
        Hover,
        Activated
    }

    public abstract class IconControl
    {
        private ImageBrush fillBrush;
        private Rectangle frame;
        private State state;
        private bool activates;

        private string normalStateIconSource;
        private string hoverStateIconSource;
        private string activatedStateIconSource;

        public Rectangle Frame
        {
            get { return this.frame; }
            set { this.frame = value; }
        }

        public State State 
        {
            get
            {
                return this.state;
            }
            set
            {
                string stateString = String.Empty;
                if (value == State.Normal)
                {
                    stateString = this.normalStateIconSource;
                }
                else if (value == State.Hover)
                {
                    stateString = this.hoverStateIconSource;
                }
                else
                {
                    stateString = this.activatedStateIconSource;
                }

                Uri uri = new Uri(stateString, UriKind.Relative);
                this.fillBrush.ImageSource = new BitmapImage(uri);
                this.frame.Fill = this.fillBrush;

                this.state = value;
            }
        }

        public IconControl(string normalIconSource, string hoverIconSource)
        {
            this.normalStateIconSource = normalIconSource;
            this.hoverStateIconSource = hoverIconSource;
            this.activates = false;

            this.frame = new Rectangle();
            this.fillBrush = new ImageBrush();
            this.fillBrush.Stretch = Stretch.Fill;
            this.frame.Height = 40;
            this.frame.Width = 40;
            this.State = State.Normal;
        }

        public void SetActivation(string activatedIconSource)
        {
            this.activatedStateIconSource = activatedIconSource;
            this.activates = true;
        }

        public void StopActivation()
        {
            this.activates = false;
        }

        public void StartActivation()
        {
            if (this.activatedStateIconSource == null)
            {
                throw new MissingMemberException("The Icon does not have \"Activate\" mode. Please use SetActivation()");
            }
            this.activates = true;
        }

        protected virtual void ClickedOn()
        {
            if (this.activates)
            {
                if (this.State == State.Hover)
                {
                    this.State = State.Activated;
                }
                //else
                //{
                //    this.State = State.Normal;
                //}
            }
        }

        public void SetIconToolTip(string hoverMessage)
        {
            ToolTip toolTip = new ToolTip();
            toolTip.Content = hoverMessage;
            toolTip.PlacementTarget = this.frame;
            toolTip.Placement = PlacementMode.Bottom;

            this.frame.ToolTip = toolTip;
        }
    }
}
